package initialize

import (
	ginSwagger "github.com/swaggo/gin-swagger"
	"github.com/swaggo/gin-swagger/swaggerFiles"
	"net/http"

	"github.com/gin-gonic/gin"
	"server/global"
	"server/middleware"
	"server/router"
)

// 初始化总路由

func Routers() *gin.Engine {
	var Router = gin.Default()

	// 如果想要不使用nginx代理前端网页，可以修改 web/.env.production 下的
	// VUE_APP_BASE_API = /
	// VUE_APP_BASE_PATH = http://localhost
	// 然后执行打包命令 npm run build。在打开下面4行注释
	//Router.LoadHTMLGlob("./dist/*.html") // npm打包成dist的路径
	//Router.Static("/favicon.ico", "./dist/favicon.ico")
	//Router.Static("/static", "./dist/static")   // dist里面的静态资源
	//Router.StaticFile("/", "./dist/index.html") // 前端网页入口页面

	Router.StaticFS(global.GvaConfig.Local.Path, http.Dir(global.GvaConfig.Local.Path)) // 为用户头像和文件提供静态地址
	// Router.Use(middleware.LoadTls())  // 打开就能玩https了
	global.GvaLog.Info("use middleware logger")
	// 跨域
	Router.Use(middleware.Cors()) // 如需跨域可以打开
	global.GvaLog.Info("use middleware cors")
	Router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
	global.GvaLog.Info("register swagger handler")
	// 方便统一添加路由组前缀 多服务器上线使用

	//获取路由组实例
	PublicGroup := Router.Group("")
	{
		// 健康监测
		PublicGroup.GET("/health", func(c *gin.Context) {
			c.JSON(200, "ok")
		})
		//首页访问
		PublicGroup.GET("/", func(c *gin.Context) {
			c.String(200, "你好，世界！欢迎来到我的首页！")
		})
	}
	//系统路由
	systemRouter := router.RouterGroupApp.System
	systemGroup := Router.Group("admin")
	systemGroup.Use(middleware.JwtAuth()).Use(middleware.CasbinHandler()).Use(middleware.OperationRecord())
	{
		systemRouter.InitBaseRouter(systemGroup)
		systemRouter.InitMenuGroupRouter(systemGroup)
		systemRouter.InitMenuRouter(systemGroup)
		systemRouter.InitPermRouter(systemGroup)
		systemRouter.InitRoleRouter(systemGroup)
		systemRouter.InitUserRouter(systemGroup)
		systemRouter.InitFileRouter(systemGroup)
		systemRouter.InitOperationLogRouter(systemGroup)
		systemRouter.InitJwtRouter(systemGroup)
	}
	global.GvaLog.Info("router register success")
	return Router
}
